package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationCommend;
import cn.wolfcode.luowowo.article.domain.MonthCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.service.IDestinationTemplateSearchService;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.service.ITravelTemplateSearchService;
import cn.wolfcode.luowowo.search.service.IUserInfoTemplateSearchService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller

public class DataController {
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IMonthCommendService monthCommendService;
    @Reference
    private IDestinationCommendService destinationCommendService;
    @RequestMapping("/ok")
    @ResponseBody
    public String ok() {
        StrategyComment strategyComment = new StrategyComment();
        strategyComment.setContent("this is piangyin");
        strategyCommentService.save(strategyComment);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/datainit")
    public String datainit() {
   /*     List<UserInfo> list= userInfoService.list();
        for (UserInfo u : list) {
            UserInfoTemplate tt = new UserInfoTemplate();
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoTemplateSearchService.saveOrUpdate(tt);
        }*/
        ///攻略----------------------------------------------------------------------------
       /* List<StrategyDetail> all = strategyDetailService.list();
        for (StrategyDetail detail : all) {
            StrategyTemplate t = new StrategyTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());

            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());

            //List<String> list = strategyTagService.getTagsBySDetailId(detail.getId());
            //t.setTags(list);

            Destination dest = destinationService.getCountry(detail.getDest().getId());

            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            dest = destinationService.getProvince(detail.getDest().getId());
            if(dest != null){
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }

            strategyTemplateSearchService.saveOrUpdate(t);
        }*/

        ///游记----------------------------------------------------------------------------
        /*List<Travel> ts = travelService.list();

        for (Travel t : ts) {
            TravelTemplate tt = new TravelTemplate();
            tt.setId(t.getId());
            tt.setAuthorId(t.getAuthor().getId());
            tt.setAuthorName(t.getAuthor().getNickname());
            tt.setCoverUrl(t.getCoverUrl());
            tt.setCreateTime(t.getCreateTime());
            tt.setDestId(t.getDest().getId());
            tt.setDestName(t.getDest().getName());
            tt.setReplynum(t.getReplynum());
            tt.setSummary(t.getSummary());
            tt.setTitle(t.getTitle());
            tt.setViewnum(t.getViewnum());
            travelTemplateSearchService.saveOrUpdate(tt);
        }*/
        //目的地----------------------------------------------------------------------------
        List<Destination> ds = destinationService.list();

        //将月份遍历到目的地当中
        Map<Long,String> map = new HashMap<>();
        for (Integer i =1 ;i<=12;i++){
            String num = null;
            if(i==1){
                num = "一月";
            }
            if(i==2){
                num = "二月";
            }
            if(i==3){
                num = "三月";
            }
            if(i==4){
                num = "四月";
            }
            if(i==5){
                num = "五月";
            }
            if(i==6){
                num = "六月";
            }
            if(i==7){
                num = "七月";
            }
            if(i==8){
                num = "八月";
            }
            if(i==9){
                num = "九月";
            }
            if(i==10){
                num = "十月";
            }
            if(i==11){
                num = "勾月";
            }
            if(i==12){
                num = "蛋月";
            }
            MonthCommend monthCommend = monthCommendService.selectListForMonth(i);
            for(Long id:monthCommend.getDestIds()){
                String months = map.get(id);
                if(months==null||"".equals(months.trim())){
                    map.put(id,num);
                }else{
                    months = months+num;
                    map.put(id,months);
                }
            }
        }
        //将标签遍历到目的地当中
        Map<Long,String> map1 = new HashMap<>();
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail sd:list){
            Long id = sd.getDest().getId();
            String value = map1.get(id);
            if(value == null||"".equals(value.trim())){
                map1.put(id,sd.getTheme().getName());
            }else{
                value = value+sd.getTheme().getName();
                map1.put(id,value);
            }
        }
        //将天数和简介遍历
        List<DestinationCommend> dcs = destinationCommendService.list();
        for (Destination d : ds) {
            DestinationTemplate tt = new DestinationTemplate();
            tt.setId(d.getId());
            tt.setCoverUrl(d.getCoverUrl());
            tt.setName(d.getName());
            tt.setInfo(d.getInfo());
            for(Long key:map.keySet()){
                if(d.getId().longValue()==key.longValue()){
                   tt.setMonth(map.get(key));
               }
            }
            for(Long key:map1.keySet()){
                if(d.getId().longValue()==key.longValue()){
                    tt.setTheme(map1.get(key));
                }
            }
            for (DestinationCommend dc:dcs){

                if(dc.getDestId().longValue() == d.getId().longValue()){
                    tt.setDays(dc.getDays());
                    tt.setIntro(dc.getIntro());
                }
            }
            destinationTemplateSearchService.saveOrUpdate(tt);
        }
        return "ok";
    }
}
